{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## HW Settings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "testscope = 'OPENADC'\n",
    "testplat = 'CWLITEARM'\n",
    "crypt = 'TINYAES128C'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "testscope = 'OPENADC'\n",
    "testplat = 'CWLITEXMEGA'\n",
    "crypt = 'AVRCRYPTOLIB'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "testscope = 'CWNANO'\n",
    "testplat = 'CWNANO'\n",
    "crypt = 'TINYAES128C'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Version"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "version = 'HARDWARE'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "version='SIMULATED'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SSV2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ss_ver='SS_VER_2_1'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from glob import glob\n",
    "import tests\n",
    "import shutil\n",
    "from tests.tutorials import test_notebook, clear_outputs_in_dir\n",
    "from functools import partial\n",
    "\n",
    "with tests.cd('../firmware/mcu'):\n",
    "    dirs = glob('simpleserial-base-lab*')\n",
    "    dirs.extend(glob('glitch-simple-lab*'))\n",
    "    dirs.extend(glob('simpleserial-aes-lab*'))\n",
    "    for d in dirs:\n",
    "        shutil.rmtree(d)\n",
    "        \n",
    "clear_outputs_in_dir(\"./courses/sca101\", kwargs={\"SCOPETYPE\": \"OPENADC\", \"PLATFORM\": \"CWLITEARM\", \"VERSION\": \"HARDWARE\", \"SS_VER\": \"SS_VER_1_1\"})\n",
    "clear_outputs_in_dir(\"./courses/sca101\", blacklist=r\"^SOLN.*\", kwargs={})\n",
    "\n",
    "clear_outputs_in_dir(\"./courses/fault101\", kwargs={\"SCOPETYPE\": \"OPENADC\", \"PLATFORM\": \"CWLITEARM\"}, blacklist=r\".*B.*\")\n",
    "clear_outputs_in_dir(\"./courses/fault101\", kwargs={\"SCOPETYPE\": \"CWNANO\", \"PLATFORM\": \"CWNANO\"}, blacklist=r\".*[0-9]_[0-9][^B].*\")\n",
    "\n",
    "clear_outputs_in_dir(\"./courses/sca201\", kwargs={\"SCOPETYPE\": \"OPENADC\", \"PLATFORM\": \"CWLITEARM\"}, \\\n",
    "                     blacklist=r\"^(Lab.*)|(SOLN_Lab [2-9].*)|(Extending.*)\")\n",
    "\n",
    "output_dir = os.path.abspath(os.path.join('..', 'tutorials'))\n",
    "test_notebook = partial(test_notebook, allow_errors=False, output_dir=output_dir)\n",
    "\n",
    "# make sure the images are copied over for linking\n",
    "try:\n",
    "    os.mkdir(os.path.join(output_dir, 'img'))\n",
    "except FileExistsError:\n",
    "    pass\n",
    "\n",
    "for course in glob('./courses/*'):\n",
    "    for image in glob('{}/img/*'.format(course)):\n",
    "        _, image_name = os.path.split(image)\n",
    "        print(os.path.join(output_dir, 'img', image_name))\n",
    "        shutil.copyfile(image, os.path.join(output_dir, 'img', image_name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Test SCA101\n",
    "test_notebook('courses/sca101/SOLN_Lab 2_1B - Power Analysis for Password Bypass.ipynb', SCOPETYPE=testscope, PLATFORM=testplat, VERSION=version, SS_VER=ss_ver)\n",
    "print(\"Passed Lab 2_1\")\n",
    "#assert 1 == 2\n",
    "test_notebook('courses/sca101/SOLN_Lab 3_1 - Large Hamming Weight Swings.ipynb', SCOPETYPE=testscope, PLATFORM=testplat, CRYPTO_TARGET=crypt,  VERSION=version, SS_VER=ss_ver)\n",
    "print(\"Passed Lab 3_1\")\n",
    "\n",
    "test_notebook('courses/sca101/SOLN_Lab 3_2 - Recovering Data from a Single Bit.ipynb', SCOPETYPE=testscope, PLATFORM=testplat, CRYPTO_TARGET=crypt,  VERSION=version, SS_VER=ss_ver)\n",
    "print(\"Passed Lab 3_2\")\n",
    "\n",
    "#test_notebook('courses/sca101/SOLN_Lab 3_3 - DPA on Firmware Implementation of AES.ipynb', SCOPETYPE=testscope, PLATFORM=testplat, CRYPTO_TARGET=crypt,  VERSION=version)\n",
    "print(\"Passed Lab 3_3\")\n",
    "\n",
    "test_notebook('courses/sca101/SOLN_Lab 4_1 - Power and Hamming Weight Relationship.ipynb', SCOPETYPE=testscope, PLATFORM=testplat, CRYPTO_TARGET=crypt,  VERSION=version, SS_VER=ss_ver)\n",
    "print(\"Passed Lab 4_1\")\n",
    "\n",
    "test_notebook('courses/sca101/SOLN_Lab 4_2 - CPA on Firmware Implementation of AES.ipynb', SCOPETYPE=testscope, PLATFORM=testplat, CRYPTO_TARGET=crypt,  VERSION=version, SS_VER=ss_ver, N=100)\n",
    "print(\"Passed Lab 4_2\")\n",
    "\n",
    "#can't test\n",
    "#test_notebook('courses/sca101/Lab 4_3 - ChipWhisperer Analyzer CPA Attack (MAIN).ipynb', SCOPETYPE=testscope, PLATFORM=testplat, CRYPTO_TARGET=crypt,  VERSION=version)\n",
    "#print(\"Passed Lab 4_3\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_notebook('courses/sca101/SOLN_Lab 4_2 - CPA on Firmware Implementation of AES.ipynb', SCOPETYPE=testscope, PLATFORM=testplat, CRYPTO_TARGET=crypt,  VERSION=version, SS_VER=ss_ver)\n",
    "print(\"Passed Lab 4_2\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_notebook('courses/sca101/SOLN_Lab 3_3 - DPA on Firmware Implementation of AES.ipynb', SCOPETYPE=testscope, PLATFORM=testplat, CRYPTO_TARGET=crypt,  VERSION=version)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_notebook('courses/fault101/SOLN_Fault 1_1 - Introduction to Clock Glitching.ipynb', SCOPETYPE=testscope, PLATFORM=testplat)\n",
    "test_notebook('courses/fault101/SOLN_Fault 1_2 - Clock Glitching to Bypass Password.ipynb', SCOPETYPE=testscope, PLATFORM=testplat)\n",
    "test_notebook('courses/fault101/SOLN_Fault 1_3 - Clock Glitching to Memory Dump.ipynb', SCOPETYPE=testscope, PLATFORM=testplat)\n",
    "test_notebook('courses/fault101/SOLN_Fault 2_1 - Introduction to Voltage Glitching.ipynb', SCOPETYPE=testscope, PLATFORM=testplat)\n",
    "test_notebook('courses/fault101/SOLN_Fault 2_2 - Voltage Glitching to Bypass Password.ipynb', SCOPETYPE=testscope, PLATFORM=testplat)\n",
    "test_notebook('courses/fault101/SOLN_Fault 2_3 - Voltage Glitching to Memory Dump.ipynb', SCOPETYPE=testscope, PLATFORM=testplat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_notebook('courses/fault101/SOLN_Fault 2_1B - Introduction to Voltage Glitching with CWNano.ipynb', SCOPETYPE=testscope, PLATFORM=testplat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_notebook('courses/fault101/SOLN_Fault 2_2B - Voltage Glitching with CWNano to Bypass Password.ipynb', SCOPETYPE=testscope, PLATFORM=testplat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#test_notebook('courses/sca201/SOLN_Lab 1_1A - Resychronizing Traces with Sum of Absolute Difference.ipynb', SCOPETYPE=testscope, PLATFORM=testplat, VERSION=version)\n",
    "test_notebook('courses/sca201/SOLN_Lab 1_1B - Resychronizing Traces with Dynamic Time Warp.ipynb', SCOPETYPE=testscope, PLATFORM=testplat, VERSION=version)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
